<?php
class Abroadcasterv2 {
	public static function main($funcode) {
		sview::verify(true, true);
		switch ($funcode) {
			case 1:
				self::__myrtmp();
				break;
			case 2:
				self::__dostart();
				break;
			case 3:
				self::__dostop();
				break;
			case 4:
				self::__dostatus();
				break;
			case 5:
				self::__doreset();
				break;
			default:
				sview::$view->assign_error(404, lang::text(404));
		}
	}
	
	//获取推流地址5-6-5 重置直播间
	public static function __doreset() {
		$acl    = acl::getinst('vedio');
		$userid = $acl->user['id'];
		$oltools= stripos($_SERVER['HTTP_USER_AGENT'], 'ios')? 'ios':'android';
		$dataset= api_event::reset_rtmp($userid, $oltools, sview::$view);
		sview::$view->assign('datasets', $dataset);
		sview::$view->display();
	}

	//获取推流地址5-6-1
	public static function __myrtmp() {
		$acl    = acl::getinst('vedio');
		$userid = $acl->user['id'];
		$across = (int)sview::$ctrl['across'];
		$oltools= stripos($_SERVER['HTTP_USER_AGENT'], 'ios')? 'ios':'android';
		$studio = api_event::create(sview::$view, $userid, $oltools, $across);
		$dataset= array('eventid'=>(int)$studio['attachs']['eventid'], 'rtmps'=>$studio['attachs']['pushrtmp']);
		sview::$view->assign('datasets', $dataset);
		sview::$view->display();
	}
	
	//开始直播修改状态5-6-2
	public static function __dostart() {
		$acl    = acl::getinst('vedio');
		$userid = $acl->user['id'];
		$evtid  = (int)sview::$ctrl['eventid'];
		api_event::update_status(sview::$view, $evtid, 1, $userid);
		sview::$view->display();
	}
	
	//结束直播修改状态5-6-3
	public static function __dostop() {
		$acl    = acl::getinst('vedio');
		$userid = $acl->user['id'];
		$evtid  = (int)sview::$ctrl['eventid'];
		api_event::update_status(sview::$view, $evtid, 0, $userid);
		sview::$view->display();
	}
	
	//直播心跳5-6-4
	public static function __dostatus() {
		$acl      = acl::getinst('vedio');
		$userid   = $acl->user['id'];
		$evtid    = (int)sview::$ctrl['eventid'];
		$studioid = (int)sview::$ctrl['studioid'];
		$studio   = model::load('live/studio')->get_one($studioid);
		if (!$studio || $studio['userid'] != $userid) {
			sview::$view->assign_error(80081, lang::text(80081));
		}
		if ($evtid != $studio['evtid']) {
			sview::$view->assign_error(80120, lang::text(80120));
		}
		$oltools  = stripos($_SERVER['HTTP_USER_AGENT'], 'ios')? 'ios':'android';
		api_event::heart_beat(sview::$view, $studio, $oltools, $evtid, $userid);
		sview::$view->display();
	}
}